package de.poketradeshop.ui.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

/**
 * Represents a product container. Set and return a list of products.
 * 
 * @see ProductController
 */
@Named
@SessionScoped
public class ProductContainer implements Serializable {

	private List<Product> products;
	private List<Product> productDetail;
	private int size;
	private List<Integer> pages;
	private int page = 1;
	private int start = 0;
	private int end = 4;
	private int max = 4;

	public String setValue(int a, int b, int c, int d){
		page = a;
		start = b;
		end = c;
		max = d;
		
		if (d==12){
			return "offerTiles.xhtml?faces-redirect=true";
		}
		
		if (d==4){
			return "offerList.xhtml?faces-redirect=true";
		}
		
		return "";
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public List<Integer> getPages() {
		List<Integer> result = new ArrayList<Integer>();
		for (int k = 0; k < getSize(); k++) {
			if (k % max == 0)
				result.add((k / max) + 1);
		}
		if (result.size() == 1) {
			return null;
		} else {
			return result;
		}
	}

	public void setPages(List<Integer> pages) {
		this.pages = pages;
	}

	public int getSize() {
		return products.size();
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
		setStart((max * page) - max);
		setEnd(getStart() + max);
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getEnd() {
		if (getSize() < max) {
			setEnd(getSize());
			return getSize();
		}
		return end;
	}

	public void setEnd(int end) {
		if (getSize() < end) {
			this.end = getSize();
		} else {
			this.end = end;
		}
	}

	public void forward() {
		if (isForwardEnabled()) {
			setPage(getPage() + 1);
		}
	}

	public void backward() {
		if (isBackwardEnabled()) {
			setPage(getPage() - 1);
		}
	}

	public boolean isActive(int value) {
		if (value == getPage()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isVisible() {
		if (getSize() > max) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isForwardEnabled() {
		if ((getPage() + 1) <= getPages().size()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isBackwardEnabled() {
		if ((getPage() - 1) > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<Product> getProductDetail() {
		return productDetail;
	}

	public void setProductDetail(List<Product> productDetail) {
		this.productDetail = productDetail;
	}
}
